﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Elective1_kim_sequel
{
    public partial class Form_Edit : Form
    {
        string key;
        public Form_Edit(DataGridView datagridview)
        {
            InitializeComponent();
            txt_lname.Text = datagridview["Last_Name", datagridview.CurrentRow.Index].Value.ToString();
            txt_fname.Text = datagridview["First_Name", datagridview.CurrentRow.Index].Value.ToString();
            txt_mname.Text = datagridview["Middle_Name", datagridview.CurrentRow.Index].Value.ToString();
            txt_user.Text = datagridview["User_Name", datagridview.CurrentRow.Index].Value.ToString();
            txt_pass.Text = datagridview["Password", datagridview.CurrentRow.Index].Value.ToString();
            btn_ok.Click += new EventHandler(btn_ok_Click);
            key = datagridview["ID", datagridview.CurrentRow.Index].Value.ToString();
            this.Load += new EventHandler(Form_Edit_Load);
            btn_cancel.Click += new EventHandler(btn_cancel_Click);
        }

        void Database_Query_Completed(object sender, AsyncCompletedEventArgs e)
        {
            MessageBox.Show("Record Updated!");
            Database.Query_Completed -= new AsyncCompletedEventHandler(Database_Query_Completed);
            this.Close();
        }

        void btn_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        void Form_Edit_Load(object sender, EventArgs e)
        {
            if (Employee._AccessLevel == Employee.AccessLevel.User)
            {
                txt_fname.Enabled = false;
                txt_lname.Enabled = false;
                txt_mname.Enabled = false;
                txt_user.Enabled = false;
            }
        }

        void btn_ok_Click(object sender, EventArgs e)
        {
            string lname = txt_lname.Text.ToString();
            string fname = txt_fname.Text.ToString();
            string mname = txt_mname.Text.ToString();
            string user = txt_user.Text.ToString();
            string pwd = txt_pass.Text.ToString();

            if (lname.Trim() != "" || fname.Trim() != "" || mname.Trim() != "" || user.Trim() != "" || pwd.Trim() != "")
            {
                string query = "Last_Name = '" + lname + "', First_Name = '" + fname + "', Middle_Name = '" + mname + "', User_Name = '" + user + "', Password = '" + pwd + "'";
                Database.Query_Completed += new AsyncCompletedEventHandler(Database_Query_Completed);
                Database.Query_Async("UPDATE tbl1 SET " + query + " WHERE ID = '" + key + "' LIMIT 1");
            }

            else { MessageBox.Show("Please fill all the fields", "OOPS!", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }
    }
}
